﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace com.YiXiong.XML
{
    /// <summary>
    /// XML助手类
    /// </summary>
    public class XMLHelper
    {
        private string xmlfile;

        private string version = "1.0";

        private Encoding encode=Encoding.UTF8;

        private XmlDocument document = new XmlDocument(); 


        /// <summary>
        /// 创造XMLHelper对象
        /// </summary>
        /// <param name="file">文件名</param>
        /// <param name="mode">文件打开方式</param>
        /// <param name="encode">编码</param>
        public XMLHelper(string file,FileMode mode,Encoding encode)
        {
             
            this.xmlfile = file;
            switch(mode)
            {
                case FileMode.Create:
                    {
                        CreateFile(file,encode);
                        break;
                    }
                case FileMode.Open:
                    {
                        LoadFile(file);
                        break;
                    }
                case FileMode.OpenOrCreate:
                    {
                        if(File.Exists(file))
                        {
                            LoadFile(file);
                        }
                        else
                        {
                            CreateFile(file, encode);
                        }
                        break;
                    }
            }
        }

        /// <summary>
        ///  创造XMLHelper对象
        ///  文件打开方式默认为:OpenOrCreate
        ///  文件编码为:Default
        /// </summary>
        /// <param name="file">目标文件</param>
        public XMLHelper(string file) : this(file, FileMode.OpenOrCreate, Encoding.UTF8) { }

        /// <summary>
        /// 载入XML文件
        /// </summary>
        /// <param name="file"></param>
        public void LoadFile(string file)
        {
            try
            {
                document.Load(file);
            }
            catch(Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="encode"></param>
        public void CreateFile(string file, Encoding encode)
        {
            try
            {
                XmlDeclaration dec = document.CreateXmlDeclaration(version, encode.BodyName, null);
                document.AppendChild(dec);
                //document.Save(file);
            }
            catch(Exception e)
            {
                throw e; 
            }
        }

        public void CreateRootNode(string rootNode)
        {
            if (document.SelectSingleNode(rootNode)==null)
            {
                XmlElement root = document.CreateElement(rootNode);
                document.AppendChild(root);
            }
        }

        public void AddNode(string parent, string nodeName, string nodeValue, XmlAttributeCollection attributes)
        {
            //添加节点
            XmlNode parentNode = document.SelectSingleNode(parent);
            if (parentNode == null) //throw new Exception(string.Format("父节点:{0}没有找到", parent));
            {
                CreateRootNode(parent);
                parentNode = document.SelectSingleNode(parent);
            }
            XmlElement node = document.CreateElement(nodeName);
            if (attributes != null)
            {
                foreach (XmlAttribute a in attributes)
                {
                    node.SetAttribute(a.Name, a.Value);
                }
            }
            node.InnerText = nodeValue;
            parentNode.AppendChild(node);

        }

 

        /// <summary>
        /// 对指定的父节点添加子节点
        /// </summary>
        /// <param name="parent">父节点</param>
        /// <param name="nodeName">节点名</param>
        /// <param name="nodeValue">节点值</param>
        /// <param name="elementName">元素名</param>
        /// <param name="elementValue">元素值</param>

        public void AddNode(string parent, string nodeName,string nodeValue,string elementName,string elementValue)
        {
            //添加节点
            XmlNode parentNode = document.SelectSingleNode(parent);
            if (parentNode == null) throw new Exception(string.Format("父节点:{0}没有找到",parent));
            XmlElement node = document.CreateElement(nodeName);
            node.InnerText = nodeValue;
            parentNode.AppendChild(node);
            //添加元素
            XmlElement element = document.CreateElement(elementName);
            element.InnerText = elementValue;
            node.AppendChild(element);
        }

        /// <summary>
        /// 获得子节点的元素
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public string[] GetChildNodesInnerText(string nodeName)
        {
            XmlNodeList list= document.SelectSingleNode(nodeName).ChildNodes;
            string txt = string.Empty;
            foreach (XmlNode node in list)
            {

                txt =txt+ node.InnerText + ",";
            }
            return txt.Split(',');
        }

        /// <summary>
        /// 获得指定节点的属性
        /// </summary>
        /// <param name="nodeName"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public XmlAttribute GetNodeAttributes(string nodeName,string attributeName)
        {
            XmlNode node = document.SelectSingleNode(nodeName);
           
            foreach (System.Xml.XmlAttribute att in node.Attributes)
            {
                if (att.Name == attributeName)
                {
                    return new XmlAttribute(att.Name, att.Value);
                }
               
            }
            return null;
 
        }

        /// <summary>
        /// 获得指定节点的子节点属性
        /// </summary>
        /// <param name="parentNodeName"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public XmlAttributeCollection GetChildNodesAttributesByAttributeName(string parentNodeName, string attributeName)
        {
            XmlNodeList list = document.SelectSingleNode(parentNodeName).ChildNodes;
            XmlAttributeCollection atts = new XmlAttributeCollection();
            foreach (XmlNode node in list)
            {
                if (node.Attributes[attributeName] != null)
                {
                    atts.Add(new XmlAttribute(node.Attributes[attributeName].Name,node.Attributes[attributeName].Value));
                }
                
            }
            return atts;
        }

        /// <summary>
        /// 返回节点属性
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public XmlAttributeCollection GetNodeAttributes(string nodeName)
        {
            XmlNode node=   document.SelectSingleNode(nodeName);
            XmlAttributeCollection atts=new XmlAttributeCollection();
           
            foreach(System.Xml.XmlAttribute att in node.Attributes)
            {
                 atts.Add(new XmlAttribute(att.Name,att.Value));
            }             
            return atts;
        }




        /// <summary>
        /// 对指的节点添加元素
        /// </summary>
        /// <param name="nodename">目标节点</param>
        /// <param name="elementName">元素名</param>
        /// <param name="elementValue">元素值</param>
        /// <param name="arrtibuteName">属性名</param>
        /// <param name="arrtibuteValue">属性值</param>
        public void AddElement(string nodename, string elementName, string elementValue,string arrtibuteName,string arrtibuteValue)
        {

            XmlNode node = document.SelectSingleNode(nodename);

            XmlElement element = document.CreateElement(elementName);
            element.SetAttribute(arrtibuteName, arrtibuteValue);
            element.InnerText = elementValue;
            node.AppendChild(element);
 
        }

        /// <summary>
        /// 对指的节点添加元素
        /// </summary>
        /// <param name="nodename">目标节点</param>
        /// <param name="elementName">元素名</param>
        /// <param name="elementValue">元素值</param>
        /// <param name="attributes">元素属性</param>
  
        public void AddElement(string nodename, string elementName, string elementValue,XmlAttributeCollection attributes)
        {

            XmlNode node = document.SelectSingleNode(nodename);

            XmlElement element = document.CreateElement(elementName);
            if (attributes != null)
            {
                foreach (XmlAttribute a in attributes)
                {
                    element.SetAttribute(a.Name, a.Value);
                }
            }
            element.InnerText = elementValue;
            node.AppendChild(element);
        }


        public bool ExistNode(string nodeName)
        {
            XmlNode node = document.SelectSingleNode(nodeName);
            return (node == null) ? false : true;

        }

        public void Save()
        {
            document.Save(xmlfile);
           
        }









    }

}
